package Draw0513;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class DrawUIV2 extends JFrame {
    DrawListenerV2 dl = new DrawListenerV2();
    public void showUI(){
        this.setTitle("画图板V2.0");
        this.setSize(800,700);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setLayout(new FlowLayout());

        String[] strs = {"直线","矩形","圆形", "实心矩形", "实心圆形",
                "等腰三角形", "三角形", "多边形", "橡皮擦", "保存",
                "递归仿真峰线", "谢尔宾斯基矩形", "谢尔宾斯基三角形", "谢尔宾斯基圆形",
                "测试分形"
        };

        for (int i = 0; i < strs.length; i++) {
            JButton btn = new JButton();
            btn.setText(strs[i]);
            this.add(btn);
            btn.addActionListener(dl);//一开始没加，导致g为空
        }

        Color[] colors = {
                Color.BLACK, Color.WHITE, Color.GRAY, Color.LIGHT_GRAY, Color.DARK_GRAY,
                Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, Color.YELLOW, Color.MAGENTA,
                Color.PINK, Color.CYAN, new Color(166, 85, 0)
        };

        Dimension cbtnDim = new Dimension(30, 30);//统一大小

        for (int i = 0; i < colors.length; i++) {
            JButton btn = new JButton();
            btn.setBackground(colors[i]);
            btn.setPreferredSize(cbtnDim);
            this.add(btn);
            btn.addActionListener(dl);
        }

        this.setVisible(true);

        addMouseListener(dl);

        Graphics g = this.getGraphics();
        dl.g = g;
        dl.jf = this;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);// 窗体原本就需要执行的方法
        // 遍历 dl.shapeList
        BufferedImage img = new BufferedImage(getWidth(), getHeight(), 2);
        Graphics bg = img.getGraphics();// 绘制到图片上的画笔
        for (int i = 0; i < dl.shapeV2Size; i++) {
            ShapeV2 shape = dl.shapeV2List[i];
            shape.drawShapeV2(bg);
        }
        g.drawImage(img,0,0,null);
    }

    public static void main(String[] args) {
        DrawUIV2 ui = new DrawUIV2();
        ui.showUI();
    }
}
